;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.SparePartsOrderAccuracy", [])
        .controller("SparePartsOrderAccuracyCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            // tab标签展示布尔值
            $scope.activeFlag = [true, false, false];
            // 按钮权限
            $scope.btnPermission = {
                saleOrderExport: true,
                saleOrderRemoveCache: true
            };
            var pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 代理商满足率分页信息
            $scope.agentPageInfo = angular.copy(pageInfo);
            // 调度员满足率分页信息
            $scope.dispatchPageInfo = angular.copy(pageInfo);
            // 采购组满足率分页信息
            $scope.purchasePageInfo = angular.copy(pageInfo);
            // 导出功能信息
            $scope.exportParam = {
                agentUrl: ybHttp.RequestUrl("device", "/report/exportSparePartsAgent"),
                dispatchUrl: ybHttp.RequestUrl("device", "/report/exportSparePartsDispatcher"),
                purchaseUrl: ybHttp.RequestUrl("device", "/report/exportSparePartsPurchasGroup")
            };
            // 搜索条件
            $scope.condition = {
                agent: {},
                dispatch: {},
                purchase: {}
            };
            // 数据列表
            $scope.dataList = {
                agent: [],
                dispatch: [],
                purchase: []
            };
            // 高级搜索和配置表格项的展示与隐藏
            $scope.toolbarConfig = {
                showAgentColConfig: false,
                showDispatchColConfig: false,
                showPurchaseColConfig: false,
                showAgentAdSearch: false,
                showDispatchAdSearch: false,
                showPurchaseAdSearch: false
            };

            $scope.agentCol = {
                name: "代理商",
                dataField: "name",
                width: 140,
                isShow: true
            };

            $scope.dispatchCol = {
                name: "调度员",
                dataField: "name",
                width: 140,
                isShow: true
            };

            $scope.purchaseCol = {
                name: "采购组",
                dataField: "name",
                width: 140,
                isShow: true
            };

            // 公共表格头信息
            $scope.tbConf = {
                scrollWidth: 980,
                tableCol: [
                    {
                        name: "月度订单",
                        dataField: "monthOrderCount",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "紧急订单",
                        dataField: "urgentOrderCount",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "月度缺料",
                        dataField: "monthOrderLackCount",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "紧急缺料",
                        dataField: "urgentOrderLackCount",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "月度满足率",
                        dataField: "monthOrderLackRate",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "紧急满足率",
                        dataField: "urgentOrderRate",
                        width: 140,
                        isShow: true
                    }
                ]
            };

            // 时间选择器配置
            $scope.dateOptions = {
                minMode:'year',
                datepickerMode: 'year'
            };

            // 半年度选择项
            $scope.halfYearOptions = [
                {value: 1, name: '上半年'}, {value: 2, name: '下半年'}
            ];

            // 季度选择项
            $scope.seasonOptions = [
                {value: 1, name: '一季度'}, {value: 2, name: '二季度'}, {value: 3, name: '三季度'}, {value: 4, name: '四季度'}
            ];

            // 月度选择项
            $scope.monthOptions = [
                {value: 1, name: '1 月'}, {value: 2, name: '2 月'}, {value: 3, name: '3 月'}, {value: 4, name: '4 月'},
                {value: 5, name: '5 月'}, {value: 6, name: '6 月'}, {value: 7, name: '7 月'}, {value: 8, name: '8 月'},
                {value: 9, name: '9 月'}, {value: 10, name: '10 月'}, {value: 11, name: '11 月'}, {value: 12, name: '12 月'}
            ];

            // 重置搜索条件
            $scope.reset = function (type) {
                $scope.condition[type] = {};
            };

            // 搜索供货商列表
            $scope.searchAgent = function (curPage) {
                getAgentDataList(curPage);
            };

            // 搜索调度员列表
            $scope.searchDispatch = function (curPage) {
                getDispatchDataList(curPage);
            };

            // 搜索采购组列表
            $scope.searchPurchase = function (curPage) {
                getPurchaseDataList(curPage);
            };

            // 切换高级搜索和表格配置展示与隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig[toolbar]) {
                    $scope.toolbarConfig[toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig[toolbar] = true;
                    });
                }
            };

            // 获取代理商满意度列表
            function getAgentDataList(curPage) {
                var reqUrl = "/report/querySparePartsAgent";
                if (!!curPage) {
                    $scope.agentPageInfo.curPage = curPage;
                }
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition.agent).forEach(function (key) {
                    if (!$scope.condition.agent[key] && $scope.condition.agent[key] !== 0) delete $scope.condition.agent[key];
                });
                var param = {
                    pageNum: $scope.agentPageInfo.curPage,
                    pageSize: $scope.agentPageInfo.pageSize,
                    filters: angular.copy($scope.condition.agent)
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.makeTimeStr = $filter("YbDate")(item.makeTime, "yyyy-MM-dd HH:mm:ss");
                        item.outboundTimeStr = $filter("YbDate")(item.outboundTime, "yyyy-MM-dd HH:mm:ss");
                        item.outboundStatusStr = $filter("ProgressStatus")(item.outboundStatus, "outDepot");
                    });
                    $scope.agentPageInfo.totalRows = res.value.total;
                    $scope.agentPageInfo.totalPages = res.value.pages;
                    $scope.dataList.agent = res.value.list;
                });
            }

            // 获取调度员满意度列表
            function getDispatchDataList(curPage) {
                var reqUrl = "/report/querySparePartsDispatcher";
                if (!!curPage) {
                    $scope.dispatchPageInfo.curPage = curPage;
                }
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition.dispatch).forEach(function (key) {
                    if (!$scope.condition.dispatch[key] && $scope.condition.dispatch[key] !== 0) delete $scope.condition.dispatch[key];
                });
                var param = {
                    pageNum: $scope.dispatchPageInfo.curPage,
                    pageSize: $scope.dispatchPageInfo.pageSize,
                    filters: angular.copy($scope.condition.dispatch)
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.makeTimeStr = $filter("YbDate")(item.makeTime, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.dispatchPageInfo.totalRows = res.value.total;
                    $scope.dispatchPageInfo.totalPages = res.value.pages;
                    $scope.dataList.dispatch = res.value.list;
                });
            }

            // 获取采购组满意度列表
            function getPurchaseDataList(curPage) {
                var reqUrl = "/report/querySparePartsPurchasGroup";
                if (!!curPage) {
                    $scope.purchasePageInfo.curPage = curPage;
                }
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition.purchase).forEach(function (key) {
                    if (!$scope.condition.purchase[key] && $scope.condition.purchase[key] !== 0) delete $scope.condition.purchase[key];
                });
                var param = {
                    pageNum: $scope.purchasePageInfo.curPage,
                    pageSize: $scope.purchasePageInfo.pageSize,
                    filters: angular.copy($scope.condition.purchase)
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.makeTimeStr = $filter("YbDate")(item.makeTime, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.purchasePageInfo.totalRows = res.value.total;
                    $scope.purchasePageInfo.totalPages = res.value.pages;
                    $scope.dataList.purchase = res.value.list;
                });
            }

            var btnPermissionCode = {
                saleOutboundExport: window.ActionCode.saleOutboundExport
            };

            $scope.btnPermission = {
                saleOutboundExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);